package com.whfc.ms.api.fmam.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fmam.entity.dto.FmamWeighRoomDTO;
import com.whfc.fmam.entity.dto.FmamWeighRoomFvsDeviceDTO;
import com.whfc.fmam.entity.dto.LicenseFileDTO;
import com.whfc.fmam.entity.qo.WeighRoomAddQO;
import com.whfc.fmam.entity.qo.WeighRoomEditQO;
import com.whfc.fmam.entity.qo.WeighRoomFvsDeviceAddQO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fvs.dto.FvsRecordDTO;
import com.whfc.fvs.dto.FvsStreamDTO;
import com.whfc.ms.api.fmam.service.WeighRoomService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author sun_guodong
 * @description 磅房接口
 * @date 2019-11-13
 */
@RequiresPermissions("fmam/weighRoom")
@RestController
@RequestMapping("/ms/api/fmam/weighRoom")
public class FmamWeighRoomController extends BaseController {
    @Autowired
    private WeighRoomService weighRoomService;

    /**
     * 磅房列表页
     *
     * @param user
     * @param pageNum
     * @param pageSize
     * @param deptId
     * @return
     */
    @GetMapping(value = "/list", params = {"pageNum", "pageSize"})
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam(value = "deptId") Integer deptId) {
        logger.info("磅房列表页，loginer:{},pageNum:{},pageSize:{}.deptId:{}", user.getUsername(), pageNum, pageSize, deptId);
        PageData<FmamWeighRoomDTO> pageData = weighRoomService.listFmamWeighRoomDTO(user, pageNum, pageSize, deptId);
        return ResultUtil.success(pageData);
    }

    @GetMapping(value = "/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam(value = "deptId") Integer deptId) {
        logger.info("磅房列表页(不分页)，loginer:{},deptId:{}", user.getUsername(), deptId);
        ListData<FmamWeighRoomDTO> listData = weighRoomService.listFmamWeighRoomDTO(deptId);
        return ResultUtil.success(listData);
    }

    /**
     * 新增磅房
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("fmam/weighRoom/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @Validated @RequestBody WeighRoomAddQO request) {
        logger.info("新增磅房。loginer:{},WeighRoomAddQO:{}", user.getUsername(), request.toString());
        weighRoomService.addWeighRoom(request);
        return ResultUtil.success();
    }

    /**
     * 编辑磅房
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("fmam/weighRoom/edit")
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @Validated @RequestBody WeighRoomEditQO request) {
        logger.info("编辑磅房。loginer:{},WeighRoomEditQO:{}", user.getUsername(), request.toString());
        weighRoomService.editWeighRoom(request);
        return ResultUtil.success();
    }


    /**
     * 删除磅房
     *
     * @param user
     * @param id
     * @return
     */
    @RequiresPermissions("fmam/weighRoom/del")
    @PostMapping("/del/{id}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable("id") Integer id) {
        logger.info("删除磅房，loginer：{}，id:{}", user.getUsername(), id);
        weighRoomService.delWeighRoom(id);
        return ResultUtil.success();
    }

    /**
     * 查看磅房的授权文件
     *
     * @param user
     * @param weighRoomId
     * @return
     */
    @GetMapping("/licenseFileList")
    public Result licenseFileList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @RequestParam("weighRoomId") Integer weighRoomId) {
        logger.info("查看磅房授权文件，loginer:{},weighRoomId:{}", user.getUsername(), weighRoomId);
        ListData<LicenseFileDTO> listData = weighRoomService.licenseFileList(weighRoomId);
        return ResultUtil.success(listData);
    }

    /**
     * 生成授权文件
     *
     * @param user
     * @param id
     * @return
     */
    @PostMapping("/licenseGenerate/{id}")
    public Result licenseGenerate(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @PathVariable("id") Integer id) {
        logger.info("生成授权文件，loginer:{},id:{}", user.getUsername(), id);
        weighRoomService.licenseGenerate(id);
        return ResultUtil.success();
    }

    /**
     * 获取磅房视频监控列表
     *
     * @param weighRoomId
     * @param user
     * @return
     */
    @GetMapping("/video/list")
    public Result videoList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestParam(value = "weighRoomId") Integer weighRoomId) {
        logger.info("磅房视频列表，loginer:{},weighRoomId:{}", user.getUsername(), weighRoomId);
        List<FmamWeighRoomFvsDeviceDTO> fvsDeviceDTOS = weighRoomService.videoList(weighRoomId);
        return ResultUtil.success(fvsDeviceDTOS);
    }

    /**
     * 获取磅房未添加的项目视频监控列表
     *
     * @param weighRoomId
     * @param user
     * @return
     */
    @GetMapping("/video/project/list")
    public Result projectVideoList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                   @RequestParam("deptId") Integer deptId,
                                   @RequestParam(value = "weighRoomId") Integer weighRoomId) {
        logger.info("获取磅房未添加的项目视频监控列表，loginer:{},deptId:{},weighRoomId:{}", user.getUsername(), deptId, weighRoomId);
        List<FmamWeighRoomFvsDeviceDTO> fvsDeviceDTOS = weighRoomService.projectVideoList(deptId, weighRoomId);
        return ResultUtil.success(fvsDeviceDTOS);
    }


    /**
     * 磅房添加视频监控设备
     *
     * @param fvsDeviceAddQO
     * @param user
     * @return
     */
    @PostMapping("/video/add")
    public Result videoAdd(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @RequestBody WeighRoomFvsDeviceAddQO fvsDeviceAddQO) {
        logger.info("磅房添加视频监控设备，loginer:{},weighRoomId:{}", user.getUsername(), fvsDeviceAddQO);
        weighRoomService.addVideo(fvsDeviceAddQO);
        return ResultUtil.success();
    }

    /**
     * 磅房视频监控直播
     *
     * @param user
     * @param deviceId
     * @return
     */
    @GetMapping("/video/stream/url")
    public Result videoStreamURL(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                 @RequestParam(value = "deviceId") Integer deviceId) {
        logger.info("获取磅房视频监控直播地址，loginer:{},deviceId:{}", user.getUsername(), deviceId);
        FvsStreamDTO streamUrl = weighRoomService.videoStreamURL(deviceId);
        return ResultUtil.success(streamUrl);
    }

    /**
     * 磅房视频监控回放
     *
     * @param user
     * @param deviceId
     * @param date
     * @return
     */
    @GetMapping("/video/record/list")
    public Result videoRecordList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @RequestParam(value = "deviceId") Integer deviceId,
                                  @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        logger.info("获取磅房视频监控回放地址，loginer:{},deviceId:{},date:{}", user.getUsername(), deviceId, date);
        Date startTime = DateUtil.getDateBegin(date);
        Date endTime = DateUtil.getDateEnd(date);
        ListData<FvsRecordDTO> recordList = weighRoomService.videoRecordList(deviceId, startTime, endTime);
        return ResultUtil.success(recordList);
    }

    /**
     * 磅房视频监控设备删除
     *
     * @param user
     * @param weighRoomId
     * @param deviceId
     * @return
     */
    @PostMapping("/video/del/{weighRoomId}/{deviceId}")
    public Result videoDel(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @PathVariable("weighRoomId") Integer weighRoomId,
                           @PathVariable("deviceId") Integer deviceId) {
        logger.info("磅房视频监控设备删除，user:{},weighRoomId:{},deviceId:{}", user.getUsername(), weighRoomId, deviceId);
        weighRoomService.videoDel(weighRoomId, deviceId);
        return ResultUtil.success();
    }


}
